package com.example.spritesheetanimation.actor;

import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.Log;

public class Animation {
	private Texture texture;
	private int rows;
	private int columns;
	private int duration;
	private int frameDuration;
	private int currentFrame;
	private int x;
	private int y;
	private long lastTimeDraw;
	private int widthFrame, heightFrame;

	public Animation(Texture texture, int rows, int columns, int frameDuration) {
		super();
		this.texture = texture;
		this.rows = rows;
		this.columns = columns;
		this.frameDuration = frameDuration;
		this.duration = this.frameDuration * columns;
		this.currentFrame = 0;
		widthFrame = texture.getWidth() / columns;
		heightFrame = texture.getHeight() / rows;

	}

	public void update() {

		if (System.currentTimeMillis() - lastTimeDraw > frameDuration) {

			lastTimeDraw = System.currentTimeMillis();
			currentFrame = ++currentFrame % columns;
		}
	}

	public void onDraw(Canvas canvas, int x, int y) {

		int srcX = currentFrame * widthFrame;

		int srcY = 0 * heightFrame;

		Rect src = new Rect(srcX, srcY, srcX + widthFrame, srcY + heightFrame);
		Rect dst = new Rect(x, y, x + heightFrame, y + heightFrame);

		canvas.drawBitmap(texture.getTexture(), src, dst, null);

	}

	public Texture getTexture() {
		return texture;
	}

	public void setTexture(Texture texture) {
		this.texture = texture;
	}

	public int getRows() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	public int getColumns() {
		return columns;
	}

	public void setColumns(int columns) {
		this.columns = columns;
	}

	public int getDuration() {
		return duration;
	}

	public void setDuration(int duration) {
		this.duration = duration;
	}

	public int getFrameDuration() {
		return frameDuration;
	}

	public void setFrameDuration(int frameDuration) {
		this.frameDuration = frameDuration;
	}

}
